<?php
namespace app\index\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
class Member extends Base{
    //接收uniappAPI提交过来的以下参数：store_name,province,city,area,contact_name,mobile
    //需要做以下检测：
    //1，需要判断以上为必填字段；
    //2，检测mobile是否为正确的手机号码
    //3，验证mobile是否重复
    //如果验证不通过返回错误的状态和msg提示。验证通过则将以上信息添加到member表里。
    public function add(){
        $data = Request::param();
        $validate = new \app\index\validate\MemberValidate();
        if(!$validate->check($data)){
            return json(['status'=>0,'msg'=>$validate->getError()]);
        }
        $data['password'] = md5($data['password']);
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['group_id'] = 3;
        $res = Db::name('member')->insert($data);
        if($res){
            return json(['status'=>1,'msg'=>'添加成功']);
        }else{
            return json(['status'=>0,'msg'=>'添加失败']);
        }
    }
    //接收附件上传的图片，将图片保存到public/uploads目录下，返回图片的路径，并验证图片的大小和格式
    public function upload(){
        $file = request()->file('file');
        $validate = new \app\index\validate\FileValidate();
        if(!$validate->check( request()->file())){
            return json(['status'=>0,'msg'=>$validate->getError()]);
        }
        $path=\think\facade\Filesystem::disk('public')->putFile( 'member', $file, 'uniqid');
        if ($path) {
            $path="/upload/".$path;
            return json(['status' => 1, 'msg' => 'success', 'path' => 'http://'.$_SERVER['HTTP_HOST'].$path]);
        } else {
            return json(['status' => 0, 'msg' => $file->getError()]);
        }
    }
}